/* Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * This file is a part of the CANN Open Software.
 * Licensed under CANN Open Software License Agreement Version 1.0 (the "License").
 * Please refer to the License for details. You may not use this file except in compliance with the License.
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
 * See LICENSE in the root of the software repository for the full text of the License.
 * ===================================================================================================================*/
#include "base/context_builder/op_infer_shape_range_context_builder.h"
#include "exe_graph/runtime/infer_shape_range_context.h"
#include "base/context_builder/op_context_builder_impl.h"
#include "base/context_builder/context_holder_builder.h"
#include "common/ge_common/util.h"
#include "base/context_builder/op_info.h"
#include "base/context_builder/op_info_impl.h"
#include <memory>
#include "graph/debug/ge_util.h"

namespace gert {
class OpInferShapeRangeContextBuilderImpl : public ContextBuilderImpl {
 public:
  OpInferShapeRangeContextBuilderImpl() : ContextBuilderImpl() {}
  ~OpInferShapeRangeContextBuilderImpl() override = default;

  std::unique_ptr<ContextHolderImpl> BuildInferShapeRangeContext() {
    auto op_info_impl = OpInfoHelper::GetPtr(op_info_);
    GE_ASSERT_NOTNULL(op_info_impl, "op_info_impl is nullptr");
    GE_ASSERT_TRUE(op_info_impl->CheckParams(), "CheckParams failed, op_type: %s, op_name: %s",
                   op_info_impl->op_type_.c_str(), op_info_impl->op_name_.c_str());
    auto holder = ge::ComGraphMakeUnique<ContextHolderImpl>();
    GE_ASSERT_NOTNULL(holder, "Create ContextHolderImpl failed.");
    GE_ASSERT_SUCCESS(CreateComputeNodeInfo(*holder), "Create compute node info failed.");
    GE_ASSERT_SUCCESS(BuildCtx(*holder), "BuildCtx failed.");
    return holder;
  }
};
OpInferShapeRangeContextBuilder::OpInferShapeRangeContextBuilder()
    : impl_(ge::ComGraphMakeUnique<OpInferShapeRangeContextBuilderImpl>()) {}
OpInferShapeRangeContextBuilder::~OpInferShapeRangeContextBuilder() {};

OpInferShapeRangeContextBuilder &OpInferShapeRangeContextBuilder::InputTensors(
    const std::vector<gert::Range<gert::Tensor> *> &inputs) {
  GE_CHECK_NOTNULL_EXEC(impl_, return *this);
  std::vector<void *> tmp_inputs;
  for (auto input : inputs) {
    tmp_inputs.emplace_back(input);
  }
  impl_->Inputs(std::move(tmp_inputs));
  return *this;
}

OpInferShapeRangeContextBuilder &OpInferShapeRangeContextBuilder::OutputShapes(
    const std::vector<gert::Range<gert::Shape> *> &outputs) {
  GE_CHECK_NOTNULL_EXEC(impl_, return *this);
  std::vector<void *> tmp_outputs;
  for (auto output : outputs) {
    tmp_outputs.emplace_back(output);
  }
  impl_->Outputs(std::move(tmp_outputs));
  return *this;
}

ContextHolder<InferShapeRangeContext> OpInferShapeRangeContextBuilder::Build() {
  GE_CHECK_NOTNULL_EXEC(impl_, return ContextHolder<InferShapeRangeContext>());
  auto ctx_holder_impl = static_cast<OpInferShapeRangeContextBuilderImpl *>(impl_.get())->BuildInferShapeRangeContext();
  auto holder_void = ContextHolderBuilder::Create(std::move(ctx_holder_impl));
  return ContextHolder<InferShapeRangeContext>(std::move(holder_void));
}

OpInfo &OpInferShapeRangeContextBuilder::MutableOpInfo() {
  static OpInfo null_op_info;
  GE_CHECK_NOTNULL_EXEC(impl_, return null_op_info);
  return impl_->GetOpInfo();
}

}  // namespace gert
